package com.yykj.drup.server.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yykj.drup.server.dto.CategoryTreeDto;
import com.yykj.drup.server.mapper.CategoryDao;
import com.yykj.drup.server.model.Category;
import com.yykj.drup.server.service.CategoryService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zengg
 * @since 2022-03-10
 */
@Service
@AllArgsConstructor
public class CategoryServiceImp extends ServiceImpl<CategoryDao, Category> implements CategoryService {

    final CategoryDao categoryDao;

    @Override
    public List<CategoryTreeDto> getCategoryTree(Integer categoryType) {
        List<CategoryTreeDto> categoryTreeDto = new ArrayList<>();
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",0).eq("category_type",categoryType).eq("is_deleted",0);
        List<Category> parentCategorys = this.list(wrapper);
        if (!parentCategorys.isEmpty()){
            parentCategorys.stream().forEach(parentCategory ->{
                CategoryTreeDto categoryTree = new CategoryTreeDto();
                BeanUtil.copyProperties(parentCategory,categoryTree);
                categoryTree.setCategories(this.getCategoryByParentId(categoryTree.getId()));
                categoryTreeDto.add(categoryTree);
            });
        }
        return categoryTreeDto;
    }

    @Override
    public List<Category> getCategoryByParentId(Integer parentId) {
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",parentId).eq("is_deleted",0);
        return this.list(wrapper);
    }

    @Override
    public List<Category> getCategory(Integer categoryType) {
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.ne("parent_id",0).eq("category_type",categoryType).eq("is_deleted",0);
        return this.list(wrapper);
    }

    @Override
    public List<Category> getStoreCategory(Integer categoryType, Integer storeId) {
        return categoryDao.getStoreCategory(categoryType,storeId);
    }
}
